﻿using System;
using System.Collections.Generic;
using Framework.Resource;
using UnityEngine;
using Object = System.Object;

namespace Framework
{

    public  class GameEntry 
    {
        private static Dictionary<Type, IFrameworkComponent> m_components= new Dictionary<Type, IFrameworkComponent>();
 


        public static void RegisterComponent(IFrameworkComponent component)
        {
            Type type = component.GetType();
            if (m_components.ContainsKey(type))
            {
                return;
            }
            else
            {
                m_components.Add(type,component);
            }
        }

        public static IFrameworkComponent GetComponent(Type type)
        {
            return m_components[type];
        }
        public static T GetComponent<T>() where T : class, IFrameworkComponent
        {
            Type type = typeof(T);
            if (type.IsClass && !type.IsAbstract)
            {
                if (m_components.ContainsKey(type))
                {
                    return m_components[type] as T;
                }
            }

            return null;
        }

        public static void Update(float elapseSeconds, float realElapseSeconds)
        {
            foreach (var component in m_components)
            {
                component.Value.OnUpdate(elapseSeconds,realElapseSeconds);
            }
        }
        
        public static void Shutdown()
        {
            // if (shutdownType==ShutdownType.Quit)
            // {
            //     Application.Quit();
            //     return;
            // }
            foreach (var component in m_components)
            {
                component.Value.Shutdown();
            }
            m_components.Clear();
        }
    }

}